// Created by liusc on 2020/7/28--11:17.
import pager from '@/assets/vue-mixin/pager'
import sortTree from '@/components/sort-tree'
import {mapGetters} from "vuex"
import {fetchList, addObj, putObj, getObj, delObj, initParameter} from "@/views/wy-budget/api/budgetaddapply.js";
import {getDict} from "~/utils";
import {mapState} from 'vuex'

export default {
    name: 'resource-manage',
    mixins: [pager],
    components: {sortTree},
    data() {
        return {
            tableData: [],
            searchForm: {},
            lists:[],
            lists2:[],
            lists3:[],
            get_button: false,
            add_button: false,
            edit_button: false,
            del_button: false,
            placeholder: '请输入预算编号或者申请名称',
        }
    },

    computed: {
        ...mapGetters(['permissions']),
        ...mapState({user: state => state.user.user}),
    },

    created() {
        this.searchForm.applyType=13
        this.lists = getDict('budget_department');
        this.lists2 = getDict('budget_type');
        this.lists3 = getDict('budget_status');
    },

    mounted(){
        this.get_button = this.permissions['budgetaddapply_get']
        this.add_button = this.permissions['budgetaddapply_add']
        this.edit_button = this.permissions['budgetaddapply_edit']
        this.del_button = this.permissions['budgetaddapply_del']
    },

    methods: {

        searchByKey(val){
            this.searchForm.applyType=13
            if (val) {
                this.searchForm.budgetCode = val
            } else {
                delete this.searchForm.budgetCode
            }
            this.resetList()
        },

        getListData() {
            this.searchForm.applyType=13
            this.searchForm.applyDept=this.user.deptId
            fetchList(Object.assign({}, this.pageParams, this.searchForm)).then(resp => {
                this.setListData(resp.data)
            })
        },


        /**
         * 查询
         * @param param
         */
        query() {

            this.searchForm.applyType=13
            this.searchForm.applyDept=this.user.deptId
            fetchList(Object.assign({
                current: this.pager.currentPage,
                size: this.pager.pageSize,
            }, this.searchForm)).then((repdata) => {
                if(repdata.data.code === 0){
                    //表示请求成功
                    let repD = repdata.data.data
                    this.tableData = repD.records
                    this.pager.total = repD.total
                 }
            })
        },

        /**
         * 根据主键ID删除
         * @param scope
         */
        deleteRow(scope) {
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                delObj(scope.row.applyId).then(response => {
                    this.getListData()
                })

                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        /**
         * 生成台账
         * @param scope
         */
        toParameterData(scope) {
            initParameter(scope.row)
                .then(rep => {
                    this.alert("生成台账成功");
                })
                .catch(e => {
                    this.alert("生成台账失败");
                });
        },

        /**
         * 跳转到申报页面
         * @param scope
         */
        toEditData(scope) {
            this.$router.push({name: 'addapply_edit', params: {'applyId': scope.row.applyId,'fileId': scope.row.fileId,'fileId2': scope.row.fileId2} })
        },

        /**
         * 跳转到申报页面
         * @param scope
         */
        toEditData2(scope) {
            this.$router.push({name: 'addapply_edit2', params: {'applyId': scope.row.applyId,'fileId': scope.row.fileId,'fileId2': scope.row.fileId2} })
        },

        /**
         * 跳转到申报查看页面
         * @param scope
         */
        toLookData(scope) {
            this.$router.push({name: 'addapply_look', params: {'applyId': scope.row.applyId,'fileId': scope.row.fileId,'fileId2': scope.row.fileId2} })
        },

        /**
         * 跳转到编辑查看页面
         * @param scope
         */
        toLookData2(scope) {
            this.$router.push({name: 'addapply_look2', params: {'applyId': scope.row.applyId,'fileId': scope.row.fileId,'fileId2': scope.row.fileId2} })
        },

        dictVal_department: function(row, column, cellValue){
            let obj = this.lists.find(p => p.value == cellValue )
            if(obj){
                return obj.label
            }
            return ''
        },

        dictVal_type: function(row, column, cellValue){
            let obj = this.lists2.find(p => p.value == cellValue )
            if(obj){
                return obj.label
            }
            return ''
        },

        dictVal_status: function(row, column, cellValue){
            let obj = this.lists3.find(p => p.value == cellValue )
            if(obj){
                return obj.label
            }
            return ''
        },

    }
}
